from django.shortcuts import render
from django.views.generic import View
from django.views.decorators.http import require_POST,require_GET
from apps.course.models import Course,Category
from .forms import CourseForm
from utils import restful


class PubCourseView(View):

    def get(self,request):
        categories = Category.objects.all()
        context = {
            'categories':categories
        }
        return render(request,'cms/pub_course.html',context=context)

    def post(self,request):
        form = CourseForm(request.POST)
        print(request.POST)
        if form.is_valid():
            cleaned_data = form.cleaned_data
            category_id = cleaned_data.get('category')
            category = Category.objects.get(id=category_id)
            cleaned_data['category'] = category
            # title = cleaned_data.get('title')
            # category = cleaned_data.get('category')
            # teacher = cleaned_data.get('teacher')
            # teacher_desc = cleaned_data.get('teacher_desc')
            # video_url = cleaned_data.get('video_url')
            # thumbnail = cleaned_data.get('thumbnail')
            # price = cleaned_data.get('price')
            # duration = cleaned_data.get('duration')
            # course_desc = cleaned_data.get('course_desc')
            # catalog = cleaned_data.get('catalog')
            course = Course.objects.create(**cleaned_data)
            return restful.result(data={'pk':course.id})
        else:
            return restful.params_error(message=form.get_errors())